<template>
  <el-container class="admin-container">
    <!-- 侧边栏 -->
    <el-aside :width="isCollapse ? '64px' : '220px'" class="admin-sidebar">
      <el-menu
          background-color="#3c82f5"
          text-color="#fff"
          active-text-color="#ff0"
          :collapse="isCollapse"
          unique-opened
          router
          class="admin-menu"
      >
        <!-- 顶部Logo区域 -->
        <div class="logo-container">
          <el-avatar :src="logoUrl" class="logo" />
          <span class="logo-text" v-show="!isCollapse">管理员系统</span>
        </div>

        <!-- 菜单项 -->
        <el-sub-menu index="1">
          <template #title>
            <el-icon class="menu-icon"><User /></el-icon>
            <span>用户管理</span>
          </template>
          <el-menu-item index="/users" class="menu-item">所有用户</el-menu-item>
        </el-sub-menu>

        <el-sub-menu index="2">
          <template #title>
            <el-icon class="menu-icon"><Shop /></el-icon>
            <span>店铺管理</span>
          </template>
          <el-menu-item index="/all-stores" class="menu-item">所有店铺</el-menu-item>
          <el-menu-item index="/approve-store" class="menu-item">店铺审核</el-menu-item>
        </el-sub-menu>

        <el-sub-menu index="3">
          <template #title>
            <el-icon class="menu-icon"><Promotion /></el-icon>
            <span>助农管理</span>
          </template>
          <el-menu-item index="/stories-farmers" class="menu-item">助农故事</el-menu-item>
          <el-menu-item index="/agricultural-policy" class="menu-item">助农政策</el-menu-item>
          <el-menu-item index="/agricultural-products" class="menu-item">助农商品</el-menu-item>
        </el-sub-menu>
      </el-menu>
    </el-aside>

    <!-- 主内容区 -->
    <el-container class="admin-main-container">
      <el-header class="admin-header">
        <div class="header-left">
          <el-button @click="toggleCollapse" circle class="collapse-btn">
            <el-icon v-if="isCollapse"><Expand /></el-icon>
            <el-icon v-else><Fold /></el-icon>
          </el-button>
          <el-breadcrumb separator="/" class="breadcrumb">
            <el-breadcrumb-item v-for="item in breadcrumbItems" :key="item.path">
              {{ item.meta.title }}
            </el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <el-dropdown class="user-dropdown">
          <div class="user-info">
            <el-avatar :size="36" :src="avatarUrl" class="user-avatar" />
            <span class="user-name">管理员</span>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="logout" class="dropdown-item">
                <el-icon><SwitchButton /></el-icon>
                <span>退出登录</span>
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </el-header>

      <el-main class="admin-content">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { ref, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { User, Shop, Promotion, Expand, Fold, SwitchButton } from '@element-plus/icons-vue'

const isCollapse = ref(false)
const avatarUrl = ref('/imgs/admin/avatar.png')
const logoUrl = ref('/imgs/admin/logo.png')
const router = useRouter()
const route = useRoute()
const breadcrumbItems = ref([])

const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
}

const logout = () => {
  if (confirm('您确定要退出登录吗？')){
    localStorage.removeItem('admin')
    window.location.href = '/login'
  }
}

// 更新面包屑导航
const updateBreadcrumb = () => {
  const matched = route.matched.filter(item => item.meta && item.meta.title)
  breadcrumbItems.value = matched
}

// 初始化和路由变化时更新面包屑
updateBreadcrumb()
watch(() => route.path, updateBreadcrumb)
</script>

<style scoped>
.admin-container {
  height: 100vh;
  display: flex;
}

.admin-sidebar {
  transition: width 0.3s;
  background-color: #3c82f5;
  box-shadow: 2px 0 6px rgba(0, 21, 41, 0.15);
}

.admin-menu {
  height: 100%;
  border-right: none;
  overflow-x: hidden;
}

.logo-container {
  padding: 15px 0;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 20px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.logo {
  width: 35px;
  height: 35px;
  background-color: rgba(255, 255, 255, 0.2);
}

.logo-text {
  color: #fff;
  margin-left: 10px;
  font-weight: bold;
  font-size: 16px;
}

.menu-icon {
  font-size: 18px;
  margin-right: 8px;
}

.menu-item {
  height: 42px;
  line-height: 42px;
  font-size: 14px;
}

.admin-main-container {
  width: calc(100% - 220px);
  transition: width 0.3s;
  display: flex;
  flex-direction: column;
}

.admin-header {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  background-color: #fff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  z-index: 1;
}

.header-left {
  display: flex;
  align-items: center;
}

.collapse-btn {
  margin-right: 15px;
  color: #606266;
  background-color: #f5f7fa;
  border: none;
}

.collapse-btn:hover {
  background-color: #e6e9ed;
}

.breadcrumb {
  font-size: 14px;
}

.user-dropdown {
  cursor: pointer;
}

.user-info {
  display: flex;
  align-items: center;
}

.user-avatar {
  background-color: #f5f7fa;
}

.user-name {
  margin-left: 8px;
  font-size: 14px;
  color: #606266;
}

.dropdown-item {
  display: flex;
  align-items: center;
}

.dropdown-item .el-icon {
  margin-right: 8px;
}

.admin-content {
  padding: 20px;
  background-color: #f0f2f5;
  overflow-y: auto;
}

/* 折叠状态下的样式 */
.admin-menu--collapse .el-submenu__title span {
  display: none;
}

.admin-menu--collapse .el-submenu__icon-arrow {
  display: none;
}

.admin-menu--collapse .menu-item span {
  display: none;
}
</style>